package com.ruoyi.app.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.domain.vo.OrderProductVo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MybatisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.OrderProductMapper;
import com.ruoyi.app.domain.OrderProduct;
import com.ruoyi.app.service.IOrderProductService;
import org.springframework.util.CollectionUtils;

/**
 * 订单商品Service业务层处理
 * 
 * @author zhangkaiqiang
 * @date 2024-10-20
 */
@Service
public class OrderProductServiceImpl extends ServiceImpl<OrderProductMapper, OrderProduct> implements IOrderProductService
{
    @Autowired
    private OrderProductMapper orderProductMapper;

    /**
     * 查询订单商品
     * 
     * @param id 订单商品主键
     * @return 订单商品
     */
    @Override
    public OrderProduct selectOrderProductById(String id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询订单商品列表
     * 
     * @param orderProduct 订单商品
     * @return 订单商品
     */
    @Override
    public List<OrderProduct> selectOrderProductList(OrderProduct orderProduct)
    {
        return MybatisUtils.list(baseMapper, orderProduct);
    }

    /**
     * 根据订单ID查询商品信息
     * @param orderIds
     * @return
     */
    @Override
    public List<OrderProductVo> listByOrderIds(Collection<String> orderIds) {
        if(CollectionUtils.isEmpty(orderIds)){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<OrderProduct> lambdaQueryWrapper = new LambdaQueryWrapper<OrderProduct>();
        lambdaQueryWrapper.in(OrderProduct::getOrderId, orderIds).orderByAsc(OrderProduct::getCreateTime);
        List<OrderProduct> orderProducts = baseMapper.selectList(lambdaQueryWrapper);
        if(CollectionUtils.isEmpty(orderProducts)){
            return new ArrayList<>();
        }
        return JSONArray.parseArray(JSONArray.toJSONString(orderProducts), OrderProductVo.class);
    }
}
